package com.itsu.admin.controller.handler;

import com.itsu.admin.exception.BizException;
import com.itsu.admin.mvc.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jerry.su
 * @Date 2022/1/2 13:29
 */
@RestControllerAdvice
public class AdminControllerHandle {

    @ExceptionHandler(value = BindException.class)
    public Result handleBind(HttpServletRequest request, BindException e) {
        return Result.error(e.getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(value = BizException.class)
    public Result handleBiz(HttpServletRequest request, BizException e) {
        return Result.error(e.getCode(), e.getMessage());
    }


    @ExceptionHandler(value = Exception.class)
    public Result handleException(Exception e) {
        return Result.error(e.getMessage());
    }
}
